<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import="dao.KhachHangDAO,pojo.KhachHangPOJO"%>	
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Registration</title>
<link rel="stylesheet" type="text/css" href="css/default.css" />
<script type="text/javascript">
	function validateForm(){
		String.prototype.trim = function () {
		    return this.replace(/^\s*/, "").replace(/\s*$/, "");
		}
		
		var txtHoTen = document['registration-form'].fullName;
		var txtDiaChi = document['registration-form'].address;
		var txtEmail = document['registration-form'].email;
		
		var txtUsername = document['registration-form'].username;
		var txtPassword = document['registration-form'].password[0];
		var txtPassword2 = document['registration-form'].password[1];
		
		if (txtHoTen.value.trim() == ""){
			alert("Không được để trống họ tên.");
			txtHoTen.select();
			return false;
		}					
		
		if (txtDiaChi.value.trim() == ""){
			alert("Không được để trống địa chỉ.");
			txtDiaChi.select();
			return false;
		}
		
		if (txtEmail.value.trim() == ""){
			alert("Không được để trống email.");
			txtEmail.select();
			return false;
		}
		
		if (txtUsername.value.trim() == ""){
			alert("Không được để trống tên đăng nhập.");
			txtUsername.select();
			return false;
		}
		
		if (txtPassword.value.trim() == ""){
			alert("Không được để trống mật khẩu.");
			txtPassword.select();
			return false;
		}
		
		if (txtPassword.value.trim() != txtPassword2.value){
			alert("Mật khẩu không khớp.");
			txtPassword2.select();
			return false;
		}
		
		return true;
	}
</script>
</head>
<body>
<%@ include file="header.html"%>
<%@ include file="leftside.html"%>

<jsp:useBean id="message" class="pojo.MessagePOJO" scope="page"></jsp:useBean>

<jsp:useBean id="khachHang" class="pojo.KhachHangPOJO" scope="request"></jsp:useBean>
<jsp:setProperty property="fullName" name="khachHang" param="fullName" />
<jsp:setProperty property="address" name="khachHang" param="address" />
<jsp:setProperty property="email" name="khachHang" param="email" />
<jsp:setProperty property="username" name="khachHang" param="username" />
<jsp:setProperty property="password" name="khachHang" param="password" />

<div id="indexRightColumn" align="center">
<form name="registration-form" action="register.jsp" method="POST"
	onsubmit="return validateForm();">
<table border="0" align="center">
	<tr>
		<td colspan="2" align="center"><b>USER REGISTRATION</b></td>
	</tr>
	<tr>
		<td>Fullname</td>
		<td><input type="text" name="fullName" value="ndkhoa" /></td>
	</tr>
	<tr>
		<td>Address</td>
		<td><input type="text" name="address" value="68/52 & Dong Nai" /></td>
	</tr>
	<tr>
		<td>Email</td>
		<td><input type="text" name="email" value="ndkhoa.is@gmail.com" /></td>
	</tr>
	<tr>
		<td colspan="2">
		<hr />
		</td>
	</tr>
	<tr>
		<td>Username</td>
		<td><input type="text" name="username" value="ndkhoa" /></td>
	</tr>
	<tr>
		<td>Password</td>
		<td><input type="password" name="password" value="ndkhoa" /></td>
	</tr>
	<tr>
		<td>Re-Password</td>
		<td><input type="password" name="password" value="ndkhoa" /></td>
	</tr>
	<tr>
		<td colspan="2" align="center"><input type="submit"
			value="Register" /> <input type="reset" value="Reset" /></td>
	</tr>
</table>
</form>

<!-- Xu ly insert thong tin user moi vao database --> <%
 	if (request.getMethod().equalsIgnoreCase("POST")) { 		 		 		
 		KhachHangDAO khachHangDAO = new KhachHangDAO();
 		boolean isInserted = khachHangDAO.insert(khachHang);
 		if (isInserted == true){
 			message.setMessage("Register Sucessfully, Click <a href='login.jsp'>here</a> " + 
 			" for login.");
 			message.setType(0);
 		}else{
 			message.setMessage("Register fail, user is exist.Try again!");
 			message.setType(1);
 		}
 	}
 %>
 
 <p style="color: <%= message.getColor()%>">
 	<jsp:getProperty property="message" name="message"/>
 </p>
 
</div>

<%@ include file="footer.html"%>
</body>
</html>